#define FILE_DEBUG 1
//#define TEAM_ASSERT 

#include "public.h"
#include "lcd_spi.h"

uint16_t g_ltdc_framebuf[LCD_WIDTH][LCD_HEIGHT];//__attribute__((section(".ARM.__at_0x24000000")));//这个是RAM6编译器的代码块
uint16_t* screen_addr=(uint16_t*)((uint32_t)LCD_FRAME_BUF_ADDR);
#define RST_H 	HAL_GPIO_WritePin(LCD1_RST_GPIO_Port, LCD1_RST_Pin, GPIO_PIN_SET)
#define RST_L 	HAL_GPIO_WritePin(LCD1_RST_GPIO_Port, LCD1_RST_Pin, GPIO_PIN_RESET)

#define MOSI_H 	HAL_GPIO_WritePin(LCD1_MOSI_GPIO_Port, LCD1_MOSI_Pin, GPIO_PIN_SET)
#define MOSI_L 	HAL_GPIO_WritePin(LCD1_MOSI_GPIO_Port, LCD1_MOSI_Pin, GPIO_PIN_RESET)

#define SCLK_H 	HAL_GPIO_WritePin(LCD1_SCLK_GPIO_Port, LCD1_SCLK_Pin, GPIO_PIN_SET)
#define SCLK_L 	HAL_GPIO_WritePin(LCD1_SCLK_GPIO_Port, LCD1_SCLK_Pin, GPIO_PIN_RESET)

#define CS_H 	HAL_GPIO_WritePin(LCD1_CS_GPIO_Port, LCD1_CS_Pin, GPIO_PIN_SET)
#define CS_L 	HAL_GPIO_WritePin(LCD1_CS_GPIO_Port, LCD1_CS_Pin, GPIO_PIN_RESET)


static void lcd_delay_us(uint32_t us) 
{
//time17
//	__HAL_TIM_SetCounter(&htim17, 0);
//		__HAL_TIM_ENABLE(&htim17);
//		while(__HAL_TIM_GetCounter(&htim17) < us);
//		/* Disable the Peripheral */
//		__HAL_TIM_DISABLE(&htim17);

	uint32_t delay = us * 1680/4;
    do
    {
        __NOP();
    }
    while (delay --);

}

void lcd_spi_write_cmd(uint8_t uint8_t_writedata) 
{

    CS_L;
    lcd_delay_us(1);
    MOSI_L;
    lcd_delay_us(1);
    SCLK_H;
    lcd_delay_us(1);
    SCLK_L;
    lcd_delay_us(1);
    for (uint8_t i = 0; i < 8; i++) 
	{
        if (uint8_t_writedata & 0x80)
            MOSI_H;
        else
            MOSI_L;
        lcd_delay_us(1);
        SCLK_H;
        lcd_delay_us(1);
        uint8_t_writedata <<= 1;
        SCLK_L;
        lcd_delay_us(1);
    }
    CS_H;
    lcd_delay_us(1);
    MOSI_H;
    lcd_delay_us(10);
}

void lcd_spi_write_data(uint8_t writedata) 
{
    CS_L;
    lcd_delay_us(1);
    MOSI_H;
    lcd_delay_us(1);
    SCLK_H;
    lcd_delay_us(1);
    SCLK_L;
    lcd_delay_us(1);
    for (uint8_t i = 0; i < 8; i++) 
	{
        if (writedata & 0x80)
            MOSI_H;
        else
            MOSI_L;
        lcd_delay_us(1);
        SCLK_H;
        lcd_delay_us(1);
        writedata <<= 1;
        SCLK_L;
        lcd_delay_us(1);
    }
    CS_H;
    lcd_delay_us(1);
    MOSI_H;
    lcd_delay_us(10);
}



void lcd_init(void)
{
	HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);//开启屏幕背光
	
	RST_L;
    HAL_Delay(200);
    RST_H;
#if 0//显示ok
		lcd_spi_write_cmd(0x11); //reset command
		HAL_Delay(200);
        lcd_spi_write_cmd(0xFF);
        lcd_spi_write_data(0x77);
        lcd_spi_write_data(0x01);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x10);

        lcd_spi_write_cmd(0xC0);
        lcd_spi_write_data(0x3B);
        lcd_spi_write_data(0x00);
        
        lcd_spi_write_cmd(0xC1);
        lcd_spi_write_data(0x0D);
        lcd_spi_write_data(0x02);

        lcd_spi_write_cmd(0xC2);
        lcd_spi_write_data(0x31);
        lcd_spi_write_data(0x05);

        lcd_spi_write_cmd(0xCD);
        lcd_spi_write_data(0x08);

        lcd_spi_write_cmd(0xB0);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x11);
        lcd_spi_write_data(0x18);
        lcd_spi_write_data(0x0E);
        lcd_spi_write_data(0x11);
        lcd_spi_write_data(0x06);
        lcd_spi_write_data(0x07);
        lcd_spi_write_data(0x08);
        lcd_spi_write_data(0x07);
        lcd_spi_write_data(0x22);
        lcd_spi_write_data(0x04);
        lcd_spi_write_data(0x12);
        lcd_spi_write_data(0x0F);
        lcd_spi_write_data(0xAA);
        lcd_spi_write_data(0x31);
        lcd_spi_write_data(0x18);

        lcd_spi_write_cmd(0xB1);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x11);
        lcd_spi_write_data(0x19);
        lcd_spi_write_data(0x0E);
        lcd_spi_write_data(0x12);
        lcd_spi_write_data(0x07);
        lcd_spi_write_data(0x08);
        lcd_spi_write_data(0x08);
        lcd_spi_write_data(0x08);
        lcd_spi_write_data(0x22);
        lcd_spi_write_data(0x04);
        lcd_spi_write_data(0x11);
        lcd_spi_write_data(0x11);
        lcd_spi_write_data(0xA9);
        lcd_spi_write_data(0x32);
        lcd_spi_write_data(0x18);

        lcd_spi_write_cmd(0xFF);
        lcd_spi_write_data(0x77);
        lcd_spi_write_data(0x01);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x11);

        lcd_spi_write_cmd(0xB0);
        lcd_spi_write_data(0x60);

        lcd_spi_write_cmd(0xB1);
        lcd_spi_write_data(0x32);

        lcd_spi_write_cmd(0xB2);
        lcd_spi_write_data(0x07);

        lcd_spi_write_cmd(0xB3);
        lcd_spi_write_data(0x80);

        lcd_spi_write_cmd(0xB5);
        lcd_spi_write_data(0x49);

        lcd_spi_write_cmd(0xB7);
        lcd_spi_write_data(0x85);

        lcd_spi_write_cmd(0xB8);
        lcd_spi_write_data(0x21);

        lcd_spi_write_cmd(0xC1);
        lcd_spi_write_data(0x78);

        lcd_spi_write_cmd(0xC2);
        lcd_spi_write_data(0x78);

        lcd_spi_write_cmd(0xE0);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x1B);
        lcd_spi_write_data(0x02);
        
        lcd_spi_write_cmd(0xE1);
        lcd_spi_write_data(0x08);
        lcd_spi_write_data(0xA0);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x07);
        lcd_spi_write_data(0xA0);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x44);
        lcd_spi_write_data(0x44);

        lcd_spi_write_cmd(0xE2);
        lcd_spi_write_data(0x11);
        lcd_spi_write_data(0x11);
        lcd_spi_write_data(0x44);
        lcd_spi_write_data(0x44);
        lcd_spi_write_data(0xED);
        lcd_spi_write_data(0xA0);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0xEC);
        lcd_spi_write_data(0xA0);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x00);

        lcd_spi_write_cmd(0xE3);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x11);
        lcd_spi_write_data(0x11);

        lcd_spi_write_cmd(0xE4);
        lcd_spi_write_data(0x44);
        lcd_spi_write_data(0x44);
        
        lcd_spi_write_cmd(0xE5);
        lcd_spi_write_data(0x0A);
        lcd_spi_write_data(0xE9);
        lcd_spi_write_data(0xD8);
        lcd_spi_write_data(0xA0);
        lcd_spi_write_data(0x0C);
        lcd_spi_write_data(0xEB);
        lcd_spi_write_data(0xD8);
        lcd_spi_write_data(0xA0);
        lcd_spi_write_data(0x0E);
        lcd_spi_write_data(0xED);
        lcd_spi_write_data(0xD8);
        lcd_spi_write_data(0xA0);
        lcd_spi_write_data(0x10);
        lcd_spi_write_data(0xEF);
        lcd_spi_write_data(0xD8);
        lcd_spi_write_data(0xA0);

        lcd_spi_write_cmd(0xE6);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x11);
        lcd_spi_write_data(0x11);

        lcd_spi_write_cmd(0xE7);
        lcd_spi_write_data(0x44);
        lcd_spi_write_data(0x44);

        lcd_spi_write_cmd(0xE8);
        lcd_spi_write_data(0x09);
        lcd_spi_write_data(0xE8);
        lcd_spi_write_data(0xD8);
        lcd_spi_write_data(0xA0);
        lcd_spi_write_data(0x0B);
        lcd_spi_write_data(0xEA);
        lcd_spi_write_data(0xD8);
        lcd_spi_write_data(0xA0);
        lcd_spi_write_data(0x0D);
        lcd_spi_write_data(0xEC);
        lcd_spi_write_data(0xD8);
        lcd_spi_write_data(0xA0);
        lcd_spi_write_data(0x0F);
        lcd_spi_write_data(0xEE);
        lcd_spi_write_data(0xD8);
        lcd_spi_write_data(0xA0);

        lcd_spi_write_cmd(0xEB);
        lcd_spi_write_data(0x02);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0xE4);
        lcd_spi_write_data(0xE4);
        lcd_spi_write_data(0x88);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x40);
        
        lcd_spi_write_cmd(0xEC);
        lcd_spi_write_data(0x3C);
        lcd_spi_write_data(0x00);

        lcd_spi_write_cmd(0xED);
        lcd_spi_write_data(0xAB);
        lcd_spi_write_data(0x89);
        lcd_spi_write_data(0x76);
        lcd_spi_write_data(0x54);
        lcd_spi_write_data(0x02);
        lcd_spi_write_data(0xFF);
        lcd_spi_write_data(0xFF);
        lcd_spi_write_data(0xFF);
        lcd_spi_write_data(0xFF);
        lcd_spi_write_data(0xFF);
        lcd_spi_write_data(0xFF);
        lcd_spi_write_data(0x20);
        lcd_spi_write_data(0x45);
        lcd_spi_write_data(0x67);
        lcd_spi_write_data(0x98);
        lcd_spi_write_data(0xBA);

        lcd_spi_write_cmd(0xFF);
        lcd_spi_write_data(0x77);
        lcd_spi_write_data(0x01);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x13);

        lcd_spi_write_cmd(0xE5);
        lcd_spi_write_data(0xE4);

        lcd_spi_write_cmd(0xFF);
        lcd_spi_write_data(0x77);
        lcd_spi_write_data(0x01);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x00);

        lcd_spi_write_cmd(0x3A);
        lcd_spi_write_data(0x60);

        lcd_spi_write_cmd(0x11);

        HAL_Delay(120);

        lcd_spi_write_cmd(0x29);

        //after batchOperation
        lcd_spi_write_cmd(0x21);
        lcd_spi_write_cmd(0xFF);
        lcd_spi_write_data(0x77);
        lcd_spi_write_data(0x01);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x10);
        lcd_spi_write_cmd(0xC7);
        lcd_spi_write_data(0x00);
        lcd_spi_write_cmd(0xFF);
        lcd_spi_write_data(0x77);
        lcd_spi_write_data(0x01);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x00);
        lcd_spi_write_data(0x00);
        lcd_spi_write_cmd(0x36);
        lcd_spi_write_data(0x00); //BGR
//		lcd_spi_write_data(0x08); //RGB


#endif
#if 0
    lcd_spi_write_cmd(0xFF);
    lcd_spi_write_data(0x77);
    lcd_spi_write_data(0x01);
    lcd_spi_write_data(0x00);
    lcd_spi_write_data(0x00);
    lcd_spi_write_data(0x13);

    lcd_spi_write_cmd(0xEF);
    lcd_spi_write_data(0x08);

    //--------------------------Bank0 Setting-----------------------------------//
    //----------------------Display Control setting-----------------------------//
    lcd_spi_write_cmd(0xFF);    //BK0
    lcd_spi_write_data(0x77);
    lcd_spi_write_data(0x01);
    lcd_spi_write_data(0x00);
    lcd_spi_write_data(0x00);
    lcd_spi_write_data(0x10);

    lcd_spi_write_cmd(0xC0);    //设置分辨率
    lcd_spi_write_data(0x3b);
    lcd_spi_write_data(0x00);

    lcd_spi_write_cmd(0xC1);  //显示参数
    lcd_spi_write_data(0x0d);   //后肩VBP
    lcd_spi_write_data(0x02);   //前肩VFP

    lcd_spi_write_cmd(0xC2);    //inversion selection &frame control
    lcd_spi_write_data(0x21);
    lcd_spi_write_data(0x08);

//    lcd_spi_write_cmd(0xC3);  //配置工作模式为HV模式
//    lcd_spi_write_data(0x80);
//    lcd_spi_write_data(0x0a);
//    lcd_spi_write_data(0x0a);

    lcd_spi_write_cmd(0xCd);  //MDT设置
    lcd_spi_write_data(0x08);

	lcd_spi_write_cmd(0x3a);
	lcd_spi_write_data(0x66);  //101=16bit 110=18bit   111=24bit

	lcd_spi_write_cmd(0x36);
	lcd_spi_write_data(0x00);//8=BGR   0=RGB

    //--------------------------Gamma Cluster Setting--------------------------//
    lcd_spi_write_cmd(0xB0);
    lcd_spi_write_data(0x00); //Positive Voltage Gamma Control
    lcd_spi_write_data(0x11);
    lcd_spi_write_data(0x18);
    lcd_spi_write_data(0x0E);
    lcd_spi_write_data(0x11);
    lcd_spi_write_data(0x06);
    lcd_spi_write_data(0x07);
    lcd_spi_write_data(0x08);
    lcd_spi_write_data(0x07);
    lcd_spi_write_data(0x22);
    lcd_spi_write_data(0x04);
    lcd_spi_write_data(0x12);
    lcd_spi_write_data(0x0F);
    lcd_spi_write_data(0xAA);
    lcd_spi_write_data(0x31);
    lcd_spi_write_data(0x18);

    lcd_spi_write_cmd(0xB1);
    lcd_spi_write_data(0x00); //Negative Voltage Gamma Control
    lcd_spi_write_data(0x11);
    lcd_spi_write_data(0x19);
    lcd_spi_write_data(0x0E);
    lcd_spi_write_data(0x12);
    lcd_spi_write_data(0x07);
    lcd_spi_write_data(0x08);
    lcd_spi_write_data(0x08);
    lcd_spi_write_data(0x08);
    lcd_spi_write_data(0x22);
    lcd_spi_write_data(0x04);
    lcd_spi_write_data(0x11);
    lcd_spi_write_data(0x11);
    lcd_spi_write_data(0xA9);
    lcd_spi_write_data(0x32);
    lcd_spi_write_data(0x18);

    //-------------------------Bank1 Setting---------------------------------//
    lcd_spi_write_cmd(0xFF);
    lcd_spi_write_data(0x77);
    lcd_spi_write_data(0x01);
    lcd_spi_write_data(0x00);
    lcd_spi_write_data(0x00);
    lcd_spi_write_data(0x11);

    lcd_spi_write_cmd(0xB0);
    lcd_spi_write_data(0x60); //Vop=4.7375v

    lcd_spi_write_cmd(0xB1);
    lcd_spi_write_data(0x30); //VCOM=32

    lcd_spi_write_cmd(0xB2);
    lcd_spi_write_data(0x87); //VGH=15v

    lcd_spi_write_cmd(0xB3);
    lcd_spi_write_data(0x80);

    lcd_spi_write_cmd(0xB5);
    lcd_spi_write_data(0x49); //VGL=-10.17v

    lcd_spi_write_cmd(0xB7);
    lcd_spi_write_data(0x85);

    lcd_spi_write_cmd(0xB8);
    lcd_spi_write_data(0x21); //AVDD=6.6 & AVCL=-4.6

    lcd_spi_write_cmd(0xC1);
    lcd_spi_write_data(0x78);

    lcd_spi_write_cmd(0xC2);
    lcd_spi_write_data(0x78);

    //HAL_Delay(20);
    //----------------GIP Setting--------------------------------------//
    lcd_spi_write_cmd(0xE0);
    lcd_spi_write_data(0x00);
    lcd_spi_write_data(0x1B);
    lcd_spi_write_data(0x02);

    lcd_spi_write_cmd(0xE1);
    lcd_spi_write_data(0x08);
    lcd_spi_write_data(0xA0);
    lcd_spi_write_data(0x00);
    lcd_spi_write_data(0x00);
    lcd_spi_write_data(0x07);
    lcd_spi_write_data(0xA0);
    lcd_spi_write_data(0x00);
    lcd_spi_write_data(0x00);
    lcd_spi_write_data(0x00);
    lcd_spi_write_data(0x44);
    lcd_spi_write_data(0x44);

    lcd_spi_write_cmd(0xE2);
    lcd_spi_write_data(0x11);
    lcd_spi_write_data(0x11);
    lcd_spi_write_data(0x44);
    lcd_spi_write_data(0x44);
    lcd_spi_write_data(0xED);
    lcd_spi_write_data(0xA0);
    lcd_spi_write_data(0x00);
    lcd_spi_write_data(0x00);
    lcd_spi_write_data(0xEC);
    lcd_spi_write_data(0xA0);
    lcd_spi_write_data(0x00);
    lcd_spi_write_data(0x00);

    lcd_spi_write_cmd(0xE3);
    lcd_spi_write_data(0x00);
    lcd_spi_write_data(0x00);
    lcd_spi_write_data(0x11);
    lcd_spi_write_data(0x11);

    lcd_spi_write_cmd(0xE4);
    lcd_spi_write_data(0x44);
    lcd_spi_write_data(0x44);

    lcd_spi_write_cmd(0xE5);
    lcd_spi_write_data(0x0A);
    lcd_spi_write_data(0xE9);
    lcd_spi_write_data(0xD8);
    lcd_spi_write_data(0xA0);
    lcd_spi_write_data(0x0C);
    lcd_spi_write_data(0xEB);
    lcd_spi_write_data(0xD8);
    lcd_spi_write_data(0xA0);
    lcd_spi_write_data(0x0E);
    lcd_spi_write_data(0xED);
    lcd_spi_write_data(0xD8);
    lcd_spi_write_data(0xA0);
    lcd_spi_write_data(0x10);
    lcd_spi_write_data(0xEF);
    lcd_spi_write_data(0xD8);
    lcd_spi_write_data(0xA0);

    lcd_spi_write_cmd(0xE6);
    lcd_spi_write_data(0x00);
    lcd_spi_write_data(0x00);
    lcd_spi_write_data(0x11);
    lcd_spi_write_data(0x11);

    lcd_spi_write_cmd(0xE7);
    lcd_spi_write_data(0x44);
    lcd_spi_write_data(0x44);

    lcd_spi_write_cmd(0xE8);
    lcd_spi_write_data(0x09);
    lcd_spi_write_data(0xE8);
    lcd_spi_write_data(0xD8);
    lcd_spi_write_data(0xA0);
    lcd_spi_write_data(0x0B);
    lcd_spi_write_data(0xEA);
    lcd_spi_write_data(0xD8);
    lcd_spi_write_data(0xA0);
    lcd_spi_write_data(0x0D);
    lcd_spi_write_data(0xEC);
    lcd_spi_write_data(0xD8);
    lcd_spi_write_data(0xA0);
    lcd_spi_write_data(0x0F);
    lcd_spi_write_data(0xEE);
    lcd_spi_write_data(0xD8);
    lcd_spi_write_data(0xA0);

    lcd_spi_write_cmd(0xEB);
    lcd_spi_write_data(0x02);
    lcd_spi_write_data(0x00);
    lcd_spi_write_data(0xE4);
    lcd_spi_write_data(0xE4);
    lcd_spi_write_data(0x88);
    lcd_spi_write_data(0x00);
    lcd_spi_write_data(0x40);

    lcd_spi_write_cmd(0xEC);
    lcd_spi_write_data(0x3C);
    lcd_spi_write_data(0x00);

    lcd_spi_write_cmd(0xED);
    lcd_spi_write_data(0xAB);
    lcd_spi_write_data(0x89);
    lcd_spi_write_data(0x76);
    lcd_spi_write_data(0x54);
    lcd_spi_write_data(0x02);
    lcd_spi_write_data(0xFF);
    lcd_spi_write_data(0xFF);
    lcd_spi_write_data(0xFF);
    lcd_spi_write_data(0xFF);
    lcd_spi_write_data(0xFF);
    lcd_spi_write_data(0xFF);
    lcd_spi_write_data(0x20);
    lcd_spi_write_data(0x45);
    lcd_spi_write_data(0x67);
    lcd_spi_write_data(0x98);
    lcd_spi_write_data(0xBA);

    //--------------------------Bank3 Setting--------------------------------//
    lcd_spi_write_cmd(0xFF);
    lcd_spi_write_data(0x77);
    lcd_spi_write_data(0x01);
    lcd_spi_write_data(0x00);
    lcd_spi_write_data(0x00);
    lcd_spi_write_data(0x13);

    lcd_spi_write_cmd(0xE5);
    lcd_spi_write_data(0xE4);

    lcd_spi_write_cmd(0x11);     //sleep out
    HAL_Delay(200);
    lcd_spi_write_cmd(0x29);

	HAL_Delay(250);
#endif

#if 1
	lcd_spi_write_cmd(0x11);
	HAL_Delay(20);

	/* Write init code */
	lcd_spi_write_cmd(0xFF);
	lcd_spi_write_data(0x77);
	lcd_spi_write_data(0x01);
	lcd_spi_write_data(0x00);
	lcd_spi_write_data(0x00);
	lcd_spi_write_data(0x10);

	lcd_spi_write_cmd(0xC0);
	lcd_spi_write_data(0x3B);
	lcd_spi_write_data(0x00);

	lcd_spi_write_cmd(0xC1);
	lcd_spi_write_data(0x0D);
	lcd_spi_write_data(0x02);

	lcd_spi_write_cmd(0xC2);
	lcd_spi_write_data(0x21);
	lcd_spi_write_data(0x08);

	lcd_spi_write_cmd(0xCD);
	lcd_spi_write_data(0x18);

	lcd_spi_write_cmd(0xB0);
	lcd_spi_write_data(0x00);
	lcd_spi_write_data(0x11);
	lcd_spi_write_data(0x18);
	lcd_spi_write_data(0x0E);
	lcd_spi_write_data(0x11);
	lcd_spi_write_data(0x06);
	lcd_spi_write_data(0x07);
	lcd_spi_write_data(0x08);
	lcd_spi_write_data(0x07);
	lcd_spi_write_data(0x22);
	lcd_spi_write_data(0x04);
	lcd_spi_write_data(0x12);
	lcd_spi_write_data(0x0F);
	lcd_spi_write_data(0xAA);
	lcd_spi_write_data(0x31);
	lcd_spi_write_data(0x18);

	lcd_spi_write_cmd(0xB1);
	lcd_spi_write_data(0x00);
	lcd_spi_write_data(0x11);
	lcd_spi_write_data(0x19);
	lcd_spi_write_data(0x0E);
	lcd_spi_write_data(0x12);
	lcd_spi_write_data(0x07);
	lcd_spi_write_data(0x08);
	lcd_spi_write_data(0x08);
	lcd_spi_write_data(0x08);
	lcd_spi_write_data(0x22);
	lcd_spi_write_data(0x04);
	lcd_spi_write_data(0x11);
	lcd_spi_write_data(0x11);
	lcd_spi_write_data(0xA9);
	lcd_spi_write_data(0x32);
	lcd_spi_write_data(0x18);

	lcd_spi_write_cmd(0xFF);
	lcd_spi_write_data(0x77);
	lcd_spi_write_data(0x01);
	lcd_spi_write_data(0x00);
	lcd_spi_write_data(0x00);
	lcd_spi_write_data(0x11);

	lcd_spi_write_cmd(0xB0);
	lcd_spi_write_data(0x60);

	lcd_spi_write_cmd(0xB1);
	lcd_spi_write_data(0x30);

	lcd_spi_write_cmd(0xB2);
	lcd_spi_write_data(0x87);

	lcd_spi_write_cmd(0xB3);
	lcd_spi_write_data(0x80);

	lcd_spi_write_cmd(0xB5);
	lcd_spi_write_data(0x49);

	lcd_spi_write_cmd(0xB7);
	lcd_spi_write_data(0x85);

	lcd_spi_write_cmd(0xB8);
	lcd_spi_write_data(0x21);

	lcd_spi_write_cmd(0xC1);
	lcd_spi_write_data(0x78);

	lcd_spi_write_cmd(0xC2);
	lcd_spi_write_data(0x78);
	HAL_Delay(20);

	lcd_spi_write_cmd(0xE0);
	lcd_spi_write_data(0x00);
	lcd_spi_write_data(0x1B);
	lcd_spi_write_data(0x02);

	lcd_spi_write_cmd(0xE1);
	lcd_spi_write_data(0x08);
	lcd_spi_write_data(0xA0);
	lcd_spi_write_data(0x00);
	lcd_spi_write_data(0x00);
	lcd_spi_write_data(0x07);
	lcd_spi_write_data(0xA0);
	lcd_spi_write_data(0x00);
	lcd_spi_write_data(0x00);
	lcd_spi_write_data(0x00);
	lcd_spi_write_data(0x44);
	lcd_spi_write_data(0x44);

	lcd_spi_write_cmd(0xE2);
	lcd_spi_write_data(0x11);
	lcd_spi_write_data(0x11);
	lcd_spi_write_data(0x44);
	lcd_spi_write_data(0x44);
	lcd_spi_write_data(0xED);
	lcd_spi_write_data(0xA0);
	lcd_spi_write_data(0x00);
	lcd_spi_write_data(0x00);
	lcd_spi_write_data(0xEC);
	lcd_spi_write_data(0xA0);
	lcd_spi_write_data(0x00);
	lcd_spi_write_data(0x00);

	lcd_spi_write_cmd(0xE3);
	lcd_spi_write_data(0x00);
	lcd_spi_write_data(0x00);
	lcd_spi_write_data(0x11);
	lcd_spi_write_data(0x11);

	lcd_spi_write_cmd(0xE4);
	lcd_spi_write_data(0x44);
	lcd_spi_write_data(0x44);

	lcd_spi_write_cmd(0xE5);
	lcd_spi_write_data(0x0A);
	lcd_spi_write_data(0xE9);
	lcd_spi_write_data(0xD8);
	lcd_spi_write_data(0xA0);
	lcd_spi_write_data(0x0C);
	lcd_spi_write_data(0xEB);
	lcd_spi_write_data(0xD8);
	lcd_spi_write_data(0xA0);
	lcd_spi_write_data(0x0E);
	lcd_spi_write_data(0xED);
	lcd_spi_write_data(0xD8);
	lcd_spi_write_data(0xA0);
	lcd_spi_write_data(0x10);
	lcd_spi_write_data(0xEF);
	lcd_spi_write_data(0xD8);
	lcd_spi_write_data(0xA0);

	lcd_spi_write_cmd(0xE6);
	lcd_spi_write_data(0x00);
	lcd_spi_write_data(0x00);
	lcd_spi_write_data(0x11);
	lcd_spi_write_data(0x11);

	lcd_spi_write_cmd(0xE7);
	lcd_spi_write_data(0x44);
	lcd_spi_write_data(0x44);

	lcd_spi_write_cmd(0xE8);
	lcd_spi_write_data(0x09);
	lcd_spi_write_data(0xE8);
	lcd_spi_write_data(0xD8);
	lcd_spi_write_data(0xA0);
	lcd_spi_write_data(0x0B);
	lcd_spi_write_data(0xEA);
	lcd_spi_write_data(0xD8);
	lcd_spi_write_data(0xA0);
	lcd_spi_write_data(0x0D);
	lcd_spi_write_data(0xEC);
	lcd_spi_write_data(0xD8);
	lcd_spi_write_data(0xA0);
	lcd_spi_write_data(0x0F);
	lcd_spi_write_data(0xEE);
	lcd_spi_write_data(0xD8);
	lcd_spi_write_data(0xA0);

	lcd_spi_write_cmd(0xEB);
	lcd_spi_write_data(0x02);
	lcd_spi_write_data(0x00);
	lcd_spi_write_data(0xE4);
	lcd_spi_write_data(0xE4);
	lcd_spi_write_data(0x88);
	lcd_spi_write_data(0x00);
	lcd_spi_write_data(0x40);

	lcd_spi_write_cmd(0xEC);
	lcd_spi_write_data(0x3C);
	lcd_spi_write_data(0x00);

	lcd_spi_write_cmd(0xED);
	lcd_spi_write_data(0xAB);
	lcd_spi_write_data(0x89);
	lcd_spi_write_data(0x76);
	lcd_spi_write_data(0x54);
	lcd_spi_write_data(0x02);
	lcd_spi_write_data(0xFF);
	lcd_spi_write_data(0xFF);
	lcd_spi_write_data(0xFF);
	lcd_spi_write_data(0xFF);
	lcd_spi_write_data(0xFF);
	lcd_spi_write_data(0xFF);
	lcd_spi_write_data(0x20);
	lcd_spi_write_data(0x45);
	lcd_spi_write_data(0x67);
	lcd_spi_write_data(0x98);
	lcd_spi_write_data(0xBA);

	lcd_spi_write_cmd(0xFF);
	lcd_spi_write_data(0x77);
	lcd_spi_write_data(0x01);
	lcd_spi_write_data(0x00);
	lcd_spi_write_data(0x00);
	lcd_spi_write_data(0x00);

	lcd_spi_write_cmd(0x3A);  // Interface Pixel Format
	//lcd_spi_write_data(0x56); // RGB565
	lcd_spi_write_data(0x66); // RGB666
	//lcd_spi_write_data(0x76); // RGB888

	lcd_spi_write_cmd(0x36);
	lcd_spi_write_data(0x00);

	lcd_spi_write_cmd(0x21);

	lcd_spi_write_cmd(0x11);
	HAL_Delay(120);

	lcd_spi_write_cmd(0x29);
	HAL_Delay(20);


#endif
}

void lcd_ltdc_fill(uint16_t* addr,uint16_t color,uint32_t len)
{
	uint16_t* temp_addr=addr;
	for(uint32_t i=0;i<len;i++)
	{
		*(temp_addr+i)=color;
	}
}
void ltdc_color_fill(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t *px_map, uint32_t framebuffer)
{
    uint16_t offline;
    uint32_t addr;

    offline = 480 - (x2 - x1 + 1);   
    addr = ((uint32_t)framebuffer + 2 * (480 * y1 + x1));
	SCB_CleanInvalidateDCache();
    __HAL_RCC_DMA2D_CLK_ENABLE();                            

    DMA2D->CR &= ~(DMA2D_CR_START);                          
    DMA2D->CR = DMA2D_M2M;                                   
    DMA2D->FGPFCCR = LTDC_PIXEL_FORMAT_RGB565;                 
    DMA2D->FGOR = 0;                                         
    DMA2D->OOR = offline;                                    

    DMA2D->FGMAR = (uint32_t)px_map;                         
    DMA2D->OMAR = addr;                                     
    DMA2D->NLR = (y2 - y1 + 1) | ((x2 - x1 + 1) << 16);   
    DMA2D->CR |= DMA2D_CR_START;                             
	  
	__HAL_DMA2D_ENABLE_IT(&hdma2d,DMA2D_IT_TC);
	__HAL_DMA2D_ENABLE(&hdma2d);


} 

void LTDC_Fill(uint16_t sx,uint16_t sy,uint16_t ex,uint16_t ey,uint32_t color)
{ 
//	uint32_t psx,psy,pex,pey;	//以LCD面板为基准的坐标系,不随横竖屏变化而变化
//	uint32_t timeout=0; 
//	uint16_t offline;
//	uint32_t addr; 
//	
//		//坐标系转换
//	if(1)	//横屏
//	{
//		psx=sx;psy=sy;
//		pex=ex;pey=ey;
//	}else			//竖屏
//	{
//		psx=sy;psy=480-ex-1;
//		pex=ey;pey=480-sx-1;
//	}
//	offline=480-(pex-psx+1);
//	addr=(LCD_FRAME_BUF_ADDR+2*(480*psy+psx));
//	
//	__HAL_RCC_DMA2D_CLK_ENABLE();	//使能DM2D时钟
//	DMA2D->CR&=~(DMA2D_CR_START);	//先停止DMA2D
//	DMA2D->CR=DMA2D_R2M;			//寄存器到存储器模式
//	DMA2D->OPFCCR=LTDC_PIXEL_FORMAT_RGB565;	//设置颜色格式
//	DMA2D->OOR=offline;				//设置行偏移 
// 
//	DMA2D->OMAR=addr;				//输出存储器地址
//	DMA2D->NLR=(pey-psy+1)|((pex-psx+1)<<16);	//设定行数寄存器
//	DMA2D->OCOLR=color;						//设定输出颜色寄存器 
//	DMA2D->CR|=DMA2D_CR_START;				//启动DMA2D
//	while((DMA2D->ISR&(DMA2D_FLAG_TC))==0)	//等待传输完成
//	{
//		timeout++;
//		if(timeout>0X1FFFFF)break;	//超时退出
//	} 
//	DMA2D->IFCR|=DMA2D_FLAG_TC;		//清除传输完成标志 		
}
// 清屏
void LTDC_Clear(uint32_t color)
{
   LTDC_Fill(0,0,480-1,480-1,color);
}

